package com.zhongyouyun.wx.wxapplet.jyj.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.jyj.model.*;
import com.zhongyouyun.jyj.service.*;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.wx.wxapplet.jyj.pojo.ReportPojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("wx/wxapplet/jyj/wxApp")
@Api(value = "教育局-微信小程序功能接口", tags = {"教育局-微信小程序功能接口"})
public class WxAppletSspController {

    @Autowired
    private SspNoticeService noticeService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private DistrictService districtService;
    @Autowired
    private SspTypeService sspTypeService;
    @Autowired
    private SspContentService sspContentService;

    @GetMapping("/getContentList")
    @ResponseBody
    @ApiOperation(value = "获取所有举报内容类型", notes = "获取所有举报内容类型")
    public Result getContentList() {
        List<SspContentModel> sspTypeModels = sspContentService.selectList(new QueryWrapper<>());
        return Result.builder().message("成功").status(0).data(sspTypeModels).build();
    }

    @GetMapping("/getDistrictList")
    @ResponseBody
    @ApiOperation(value = "微信小程序获取地区", notes = "微信小程序获取地区")
    public Result getDistrictList() {
        List<DistrictModel> districtModels = districtService.selectList(new QueryWrapper<>());
        return Result.builder().message("成功").status(0).data(districtModels).build();
    }

    @GetMapping("/getTypeList")
    @ResponseBody
    @ApiOperation(value = "获取所有类型", notes = "获取所有类型")
    public Result getTypeList() {
        List<SspTypeModel> sspTypeModels = sspTypeService.selectList(new QueryWrapper<>());
        return Result.builder().message("成功").status(0).data(sspTypeModels).build();
    }

    @GetMapping("/getNotic")
    @ResponseBody
    @ApiOperation(value = "微信小程序获取公告", notes = "微信小程序获取公告")
    public Result getCode() {
        SspNoticeModel noticeModel = noticeService.getOne(new QueryWrapper<>());
        return Result.builder().message("成功").status(0).data(noticeModel).build();
    }

    @PostMapping("/saveReport")
    @ResponseBody
    @ApiOperation(value = "微信小程序投诉内容保存", notes = "微信小程序投诉内容保存")
    public Result saveReport(@RequestBody ReportPojo reportPojo) {
        if (ObjectUtil.isNotEmpty(reportPojo)) {
            Date date = new Date();
            ReportModel reportModel = new ReportModel();
            reportModel.setOpenId(reportPojo.getOpenId());
            reportModel.setName(reportPojo.getName());
            reportModel.setPosition(reportPojo.getPosition());
            reportModel.setDistrict(reportPojo.getDistrict());
            reportModel.setTrainingType(reportPojo.getTrainingType());
            reportModel.setReportName(reportPojo.getReportName());
            reportModel.setReportPhone(reportPojo.getReportPhone());
            reportModel.setReportContext(reportPojo.getReportContext());
            reportModel.setSketch(reportPojo.getSketch());
            reportModel.setState("0");
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("open_id",reportPojo.getOpenId());
            queryWrapper.orderByDesc("createTime");
            List<ReportModel> list = reportService.selectList(queryWrapper);
            if(list.size() > 0){
                ReportModel reportModel1 = list.get(0);
                long time = reportModel1.getCreateTime().getTime();
                long time1 = date.getTime();
                if((time1 - time) > 60000){
                    reportModel.setCreateTime(date);
                    reportService.saveOrUpdate(reportModel, reportPojo.getUrls());
                }else{
                    return Result.builder().message("举报限制，一分钟内只能举报一次").success(false).status(1).build();
                }
            }else{
                reportModel.setCreateTime(date);
                reportService.saveOrUpdate(reportModel, reportPojo.getUrls());
            }
            return Result.builder().message("保存成功").status(0).build();
        } else {
            return Result.builder().message("保存失败").success(false).status(1).build();
        }
    }
}
